package com.astrob.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.astrob.naviframe.Start;
import com.astrob.nhcmap.R;

public class NaviModeCtrlView extends LinearLayout {

	private Context mContext;

	// 2D + 3D + 地图指北
	private ImageButton mbtn2D = null;
	private ImageButton mbtn3D = null;
	private ImageButton mbtnNorth = null;
	
	// 指北针
	protected ImageView mCompass = null;

	public NaviModeCtrlView(Context context) {
		super(context);
		mContext = context;
		init();
	}

	public NaviModeCtrlView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		init();
	}

	private void init() {
		View convertView = View.inflate(mContext, R.layout.navimodectrl_view,
				this);
		mbtn2D = (ImageButton) findViewById(R.id.btn2D);
		mbtn3D = (ImageButton) findViewById(R.id.btn3D);
		mbtnNorth = (ImageButton) findViewById(R.id.btnNorth);
		mCompass = (ImageView) findViewById(R.id.compass_id);
		mCompass.setImageResource(R.drawable.compass00);

	}

	private static int navimode_ = 0;
	private Car mCar = null;

	public void setCar(Car car) {
		mCar = car;
		setCompass();
	}

	public void changeNavimode(int mode) {
		if (mCar == null)
			return;

		navimode_ = mode;
		if (mode == 0) {
	/*		mbtn2D.setVisibility(View.VISIBLE);
			mbtn3D.setVisibility(View.GONE);
			mbtnNorth.setVisibility(View.GONE);*/

			mCar.set3D(false);
			mCar.setCarposMode(Car.BOTTOMCENTER);
			Start.getInstance().MapKit().setMapViewMode(0);

		} else if (mode == 1) {
/*			mbtn2D.setVisibility(View.GONE);
			mbtn3D.setVisibility(View.VISIBLE);
			mbtnNorth.setVisibility(View.GONE);*/

			mCar.set3D(true);
			Start.getInstance().MapKit().setMapViewMode(1);
		} else if (mode == 2) {
/*			mbtn2D.setVisibility(View.GONE);
			mbtn3D.setVisibility(View.GONE);
			mbtnNorth.setVisibility(View.VISIBLE);*/

			mCar.set3D(false);
			mCar.setCarposMode(Car.CENTER);
			Start.getInstance().MapKit().setMapViewMode(0);
		}

		if (mCar.getCarposMode() == Car.BOTTOMCENTER) {
			double heading = mCar.getCarNavPos().dir;
			Start.getInstance().MapKit().setRotation(heading);
		} else {
			Start.getInstance().MapKit().setRotation(0.0);
		}

		// SystemSetFileHandle.getInstance().getSystemSet().naviMapViewMode =
		// navimode_;
		setCompass();
	}
	
	public void setCompass() {
		if (mCar == null)
			return;
		
		if (navimode_ == 2 || mCar.isFreeMoving()) {
			mCompass.setImageResource(R.drawable.compass00);
			return;
		}
		
		double dir = 360 - mCar.getCarNavPos().dir;
		int nIndex = (int) (dir / 11.25 + 0.5f);
		if (nIndex > 31)
			nIndex = 0;
		int cid = R.drawable.compass00 + nIndex;
		mCompass.setImageResource(cid);
	}

	public void show() {
		setVisibility(View.VISIBLE);
		changeNavimode(navimode_);
	}

	public void change() {
		if (mCar == null || mCar.isFreeMoving())
			return;
			
		if (navimode_ == 0) {
			navimode_ = 2;
		} else {
			navimode_ = 0;
		}
		changeNavimode(navimode_);		
	}

}
